﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：ChildUserAdd.cs
    // 文件功能描述：子用户，新增子用户
    // 创建标识：吕海乐2009-05-13
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;

using HZ.Supervise.Model.Enum;
using System.Text.RegularExpressions;
using HZ.Supervise;


public partial class ChildUserAdd : BasePage
{
    public ChildUserAdd()
    {

    }
    IBLL_User iu = BLLFactory.BLL_User();
    KeyOperationLogJG kj; 
    protected void Page_Load(object sender, EventArgs e)
    {
        //核心页面，使用强制权限限定
        if (!CurrentAccount.GetUser().IsAdmin)
        {
            PageError("您不是管理员，不能添加子用户！", "ChildUserList.aspx");
            return;
        }
        if (!this.IsPostBack)
        {
            //绑定默认密码
            txtChildUserPwd.Text = Constants.USER_DEFAULT_PWS;
            this.lblPrefix.Text = CurrentAccount.GetUser().UserID;

        }
    }


    /// <summary>
    /// 添加子用户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //通过session获取子用户所属机构和角色
            User sessionUser = CurrentAccount.GetUser();

            Org org = sessionUser.Org;
            Role role = sessionUser.UserRole;

            //检查用户ID规则
            if (RegexValid(txtChildUserId.Text.Trim(), "^[SJPZsjpz]{1}[0-9]{4}$"))
            {

                HZ.Web.WebUtil.Alert("子用户ID不符合规则，不能类似X0001！", this);
                return;
            }

            //获取子用户信息
            User user = new User();
            user.UserID = this.lblPrefix.Text + "_" + txtChildUserId.Text.Trim();
            user.UserName = txtChildUserName.Text.Trim();
            user.UserPassword = txtChildUserPwd.Text.Trim();
            user.IsUserAdmin = false;
            user.IsOnline = false;
            user.IsUsing = true;
            user.IsAdmin = false;
            user.UserRole = role;
            user.Org = org;
            if (cbIsJK.Checked)
            {
                user.IsJK = true;          
            }
            else
            {
                user.IsJK = false;                
            }

            //判定添加的操作情况
            IBLL_User iu = HZ.Supervise.BLLFactory.BLL_User();
            string flag = iu.AddUser_OperatingRule(user, null).ToString();
            switch (flag)
            {
                case "2":
                    PageError("此用户已经存在！", Request.RawUrl);
                    break;
                case "1":
                    int jk = iu.UpdateUser_IsJK(user);   
                    if(jk>0)
                    {
                        string strMessage = "添加成功！";
                        strMessage += "\n登录名：" + this.lblPrefix.Text + "_" + txtChildUserId.Text.Trim();
                        strMessage += "\n默认密码：" + Constants.USER_DEFAULT_PWS;
                        AddLog(string.Format("添加子用户[{0}]成功!", user.UserID));
                        PageInfo(strMessage, this.ReturnUrl);
                    }
                    else
                    {
                        PageError("添加成功，但更新【是否疾控】状态失败！", this.ReturnUrl);
                    }
                    break;
                case "0":
                    PageError("添加失败！", this.ReturnUrl);
                    break;
                default:
                    break;
            }
        }
    }
    /// <summary>
    /// 添加日志
    /// </summary>
    /// <param name="msg"></param>
    private void AddLog(string msg)
    {
        kj = new KeyOperationLogJG();
        kj.OperationLogSerialID = Guid.NewGuid().ToString();
        kj.Operator = CurrentAccount.GetUser().UserID;
        kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
        kj.OperateTime = DateTime.Now;
        kj.OperateMessage = msg;
        iu.AddKeyOperationLog(kj);
    }

    /// <summary>
    /// 匹配正则表单式
    /// </summary>
    /// <param name="input"></param>
    /// <param name="pattern"></param>
    /// <returns></returns>
    private bool RegexValid(string input, string pattern)
    {
        Regex regex = new Regex(pattern);
        if (input != null)
        {
            return regex.IsMatch(input);
        }
        else
        {
            return false;
        }
    }
}

